package com.ge.modules.sys.controller;

import com.ge.common.utils.DeleteType;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.entity.TbTechnicianEntity;
import com.ge.modules.sys.service.SysTechnicianService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 技师
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-08-07 14:42:15
 */
@RestController
@RequestMapping("/sys/technician")
public class SysTechnicianController {
    @Autowired
    private SysTechnicianService technicianService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("technician:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TbTechnicianEntity> technicianList = technicianService.queryList(query);
        int total = technicianService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(technicianList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{technicianId}")
    @RequiresPermissions("technician:info")
    public R info(@PathVariable("technicianId") Integer technicianId) {
        TbTechnicianEntity technician = technicianService.queryById(technicianId);

        return R.ok().put("technician", technician);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("technician:save")
    public R save(@RequestBody TbTechnicianEntity technician) {
        technician.setCreateTime(new Date());
        technician.setDeleteType(DeleteType.NOT_DELETE.getKey());
        technician.setUserType(2);
        //密码初始化为123456
        technician.setPassword(DigestUtils.sha256Hex("123456"));
        technicianService.save(technician);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("technician:update")
    public R update(@RequestBody TbTechnicianEntity technician) {
        technicianService.update(technician);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("technician:delete")
    public R delete(@RequestBody Integer[] technicianIds) {
        technicianService.deleteBatch(technicianIds);

        return R.ok();
    }

}
